SHEL := /bin/bash

dir_source  := src
dir_closure := closure/library

dir_test_build 				:= tests
dir_pages							:= pages

production_output_dir := build/production/

app_root := .
image_dir := assets
data_dir  := data

main: development

all : lint phantom_tests  development production

development:
	ant lint development

deps: templates
	python closure/library/closure/bin/build/depswriter.py \
		--root_with_prefix="src ../../../../src" > src/deps.js
	python closure/library/closure/bin/build/depswriter.py \
	for dir in build/templates/*/; do \
		python closure/library/closure/bin/build/depswriter.py \
		--root_with_prefix="$$dir ../../../../$$dir" \
		--root_with_prefix="closure/templating ../../../../closure/templating/" \
		> $$dir/deps.js; \
	done;

development_compile: config
	ant development

production: lint clean_production compile

compile: config
	bin/build_page \
	  --all --production --js-verbose \
	  --app-root ${app_root} \
	  --image-dir ${image_dir} \
	  --data-dir ${data_dir} \
	  -o ${production_output_dir};

jenkins: phantom_tests config
	make \
	  app_root=${production_output_dir} \
	  production_output_dir=${production_output_dir} \
	  image_dir=${production_output_dir}/assets \
	  data_dir=${production_output_dir}/data \
	  production;
	svn export data/ ${production_output_dir}/data;
	svn export assets/ ${production_output_dir}/assets;
	cp data/configs/compiled.json ${production_output_dir}/data/configs/compiled.json
	@echo "[DONE]"

templates: _ALWAYS
	@bin/build_soy;

tests:  _ALWAYS templates deps clean_tests
	bin/build_test --all

setup:
	bundle install --system
	chmod +x bin/*
	make all

telnet-test:
	bin/telnet-test 172.16.37.236 80 /betstream_js/en/index.min.js;
	bin/telnet-test 172.16.37.236 80 /betstream_js/en/index.css;

update:
	bundle update
	chmod +x bin/*

# done
fixlint:
	ant fixlint

# done
lint: _ALWAYS
	ant lint

phantom_tests: tests
	phantomjs bin/phantom_multitest.js file://$$(pwd)/tests/all-tests.html;
	phantomjs bin/phantom_multitest.js file://$$(pwd)/impltests/all-tests.html;

clean: clean_tests clean_production clean_developement clean_templates clean_deps clean_configs
	rm -rf build;

clean_deps:
	for file in "src/deps.js"; do \
		if [ -e $${file} ]; then \
			rm $${file}; \
		fi; \
	done;

clean_configs:
	if [ -e data/configs/compiled.json ]; then \
	  rm data/configs/compiled.json; \
	fi;

clean_production: clean_templates
	rm -rf ${production_output_dir}
	
clean_developement: clean_templates
	rm -rf ${development_output_dir}

clean_templates:
	rm -rf build/templates;

clean_tests: 
	find {,impl}tests -name "*_Test.html" 2>/dev/null | xargs -I % rm %;
	find {,impl}tests/all{tests.js,-tests.html} 2>/dev/null | xargs -I % rm %;

# done
config: _ALWAYS
	bin/merge_configs

_ALWAYS:
